package pl.edu.utp.kursy.database.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import pl.edu.utp.kursy.database.Company;

public class CompanyDao extends BaseDao<Company>{
    
    public CompanyDao(SessionFactory sessionFactory){
        super(sessionFactory);
    }

    @Override
    public Company getById(int id) {
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(Company.class);
        c.add(Restrictions.eq("id", id));    		    		
        Company company = (Company)c.uniqueResult();
        session.close(); 
        return company; 
    }

    @Override
    public List<Company> getAll() {
        ArrayList<Company> companies;
        Session session = sessionFactory.openSession();
        Criteria c = session.createCriteria(Company.class);
        c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        companies = (ArrayList<Company>)c.list();
        session.close();
        return companies;  
    }
    
}
